package com.soup.lab.executor.pool2;

import com.google.common.collect.Sets;

import java.util.Collection;
import java.util.Set;

public interface KeyWaitingPolicy {

    <K> boolean ifWaiting(Collection<K> targetKeys, Collection<K> waitingKeys);

    KeyWaitingPolicy DEFAULT_WAITING_POLICY = new AnyMatchPolicy();

    class AnyMatchPolicy implements KeyWaitingPolicy {

        @Override
        public <K> boolean ifWaiting(Collection<K> targetKeys, Collection<K> waitingKeys) {
            if(!(waitingKeys instanceof Set)) {
                waitingKeys = Sets.newHashSet(waitingKeys);
            }
            return targetKeys.stream().anyMatch(waitingKeys::contains);
        }
    }

}
